package wli.app.util;

import java.util.*;

public class StringStatisticsUtil {

	private String input;
	private Hashtable<String, Integer> hstb;
	private Vector<StringStatistics> vs;
	private List<StringStatistics> alist;

	public void setString(String in) throws IllegalArgumentException {
		this.input = in;
		this.input = input.toLowerCase();

		/**
		 * take input string to
		 */

		StringTokenizer stk = new StringTokenizer(this.input);
		Vector<String> v = new Vector<String>();
		hstb = new Hashtable<String, Integer>();

		String temp = null;
		int i = 0;
		while (stk.hasMoreElements()) {
			temp = (String) stk.nextElement();
			if (temp.indexOf(".") != -1) {
				temp = temp.replaceAll(".", "");
			}
			if (!v.contains(temp)) {
				v.add(temp);
				i = 1;
			} else {
				i = (Integer) hstb.get(temp);
				i++;
			}

			hstb.put(temp, i);
		}

		/**
		 * Use hashtable to set StringStatistics Vector
		 */

		Enumeration<String> emuns = (hstb.keys());

		vs = new Vector<StringStatistics>();
		alist = new ArrayList<StringStatistics>();

		StringStatistics stats = null;
		String key = null;
		int total = 0;
		while (emuns.hasMoreElements()) {
			key = emuns.nextElement();
			total = hstb.get(key);
			stats = new StringStatistics();
			stats.setName(key);
			stats.setTotal(total);
			vs.add(stats);
			alist.add(stats);
		}
	}

	public String getInputString() {
		return this.input;
	}

	public Hashtable<String, Integer> getWordsHashtable() {
		return this.hstb;
	}

	public List<StringStatistics> getWordsArrayList() {
		return this.alist;
	}
	public Vector<StringStatistics> getWordsVector() {
		return this.vs;
	}
}
